<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/29
 * Time: 18:47
 */

namespace app\api\validate;

use app\lib\exception\ParameterException;
use think\Exception;
use think\Request;
use think\Validate;

class BaseValidate extends Validate
{
    public function goCheck()
    {
        //获取http参数
        //校验
        $request = Request::instance();
        $params = $request->param();
        $result = $this->batch()->check($params);
        if (!$result){
            $e = new ParameterException([
                'msg' => $this->error,
//                'code' => 400,
//                'errorCode' => 10002
            ]);
            throw $e;
//            $error = $this->error;
//            throw new Exception($error);
        }
        else {
            return true;
        }
    }

    protected function isPositiveInteger($value, $rule = '', $data = '', $field = ''){
        if (is_numeric($value) && is_int($value + 0) && ($value + 0) > 0)
        {
            return true;
        }else{
            return false;
        }
    }
    protected  function isNotEmpty($value, $rule = '', $data = '', $field = '')
    {
        if (!empty($value)) {
            return true;
        } else {
            return false;
        }
    }

    protected function isMobile($value){
        $rule = '^1(3|4|5|7|8)[0-9]\d{8}$^';
        $result = preg_match($rule, $value);
        if (empty($result)){
            return false;
        }else{
            return true;
        }
    }

    public function getDataByRule($arrays)
    {
        if (array_key_exists('user_id', $arrays)|array_key_exists('uid', $arrays)){
            //不允许
            throw new ParameterException([
               'msg' => '参数中包含非法参数user_id,uid'
            ]);
        }

        $newArray = [];

        foreach ($this->rule as $k => $v){
            $newArray[$k] = $arrays[$k];
        }
        return $newArray;
    }


}